//
//  NoticeController.m
//  智慧小区
//
//  Created by lab603 on 15/6/30.
//  Copyright (c) 2015年 Dreamsoul. All rights reserved.
//

#import "NoticeController.h"
#import "APIClient.h"
#import "PPHttpTool.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "MBProgressHUD+MJ.h"
#import "Notice.h"
#import "NoticeFrame.h"
#import "NoticeHistoryCell.h"
#import "NoticeDetailController.h"
#import "EGOCache.h"
#import "UserInfo.h"
#define Cache_UserInfoKey @"UserInfoCache"
@interface NoticeController ()
//存放通知对象的数组
@property (nonatomic,strong)NSMutableArray *noticeArray;
@end

@implementation NoticeController
- (id)init{
    self = [super init];
    if (self) {
        self.title = @"物业通知";
        self.view.backgroundColor = APP_BACK_COLOR;
        //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

- (NSMutableArray *)noticeArray{
    if (_noticeArray==nil) {
        _noticeArray = [NSMutableArray array];
    }
    return _noticeArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //获取通知数据
    [self getNotcieData];
    //添加下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNotcieData)];
    
}
/**
 *  获取通知数据
 */
- (void)getNotcieData{
    NSString *urlString = [APIClient relativePathForNoticeHistory];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [PPHttpTool getWithURL:urlString params:params success:^(id json) {
        IWLog(@"%@",json);
        NSArray *tempArray = [Notice objectArrayWithKeyValuesArray:json];
        [self.noticeArray removeAllObjects];
        for (int i=0; i<tempArray.count; i++) {
            Notice *notice= tempArray[i];
            NoticeFrame *noticeF = [[NoticeFrame alloc] init];
            noticeF.notice = notice;
            [self.noticeArray addObject:noticeF];
        }
        if ([self.tableView.header isRefreshing]) {
            [self.tableView.header endRefreshing];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        if ([self.tableView.header isRefreshing]) {
            [self.tableView.header endRefreshing];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.noticeArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NoticeHistoryCell *cell = [NoticeHistoryCell cellWithTableView:tableView];
    NoticeFrame *noticeF = self.noticeArray[indexPath.row];
    cell.noticeFrame = noticeF;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NoticeFrame *noticeF = self.noticeArray[indexPath.row];
    return noticeF.cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NoticeFrame *noticeF = self.noticeArray[indexPath.row];
    Notice *notice = noticeF.notice;
    NoticeDetailController *detailVC = [[NoticeDetailController alloc] init];
    detailVC.notice = notice;
    [self.navigationController pushViewController:detailVC animated:YES];
    //标记通知为已读
    if ([notice.readId isEqualToString:@"0"]) {
        [self noticeDidRead:[notice.ID intValue]];
    }
}
/**
 *  标记通知已读
 */
- (void)noticeDidRead:(int)noticeId{
    UserInfo *userInfo = (UserInfo *)[[EGOCache globalCache] objectForKey:Cache_UserInfoKey];
    NSString *urlString = [APIClient relativePathForNoticeRead:noticeId];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"id"] = @(noticeId);
    [PPHttpTool postWithURL:urlString params:params success:^(id json) {
        IWLog(@"%@",json);
    } failure:^(NSError *error) {
        
    }];
}
@end
